package com.xncoding.elasticsearch.rest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xncoding.elasticsearch.config.ESUtil;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
public class RestClientController {

	@Autowired
	private RestHighLevelClient restHighLevelClient;

	@Autowired
	ESUtil esUtil;

	/**
	 * 只创建索引
	 * @return
	 */
	@RequestMapping("/cretatIndex/{esindex}")
	public boolean cretatIndex(@PathVariable("esindex") String esindex){
		return esUtil.saveIndex(esindex);
	}

	/**
	 * 只创建索引,和映射
	 * @param esindex
	 * @param settings
	 * @return
	 */
	@RequestMapping("/cretatIndex01/{esindex}")
	public boolean cretatIndex01(@PathVariable("esindex") String esindex,@RequestParam(value = "settings") String settings){
		return esUtil.saveIndex(esindex,settings);
	}

	@RequestMapping("/cretatIndex02/{esindex}")
	public String cretatIndex02(@PathVariable("esindex") String esindex,@RequestParam(value = "settings") String settings){
		return esUtil.create_index(esindex,settings);
	}

	public void batchAdd(){
		BulkRequest bulkRequest = new BulkRequest();
		List<IndexRequest> requests = new ArrayList<IndexRequest>();
		for (IndexRequest indexRequest : requests) {
			bulkRequest.add(indexRequest);
		}
		try {
			restHighLevelClient.bulk(bulkRequest,null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@RequestMapping("/putBeanMapping01/{esindex}")
	public boolean putBeanMapping01(@PathVariable("esindex") String esindex,@RequestParam(value = "settings") String settings){
		boolean index = false;
		index = esUtil.putMapping(esindex,settings);
		return index;
	}
	//---------------------------------------------------------------------

}
